#!/usr/bin/env python
#-*- encoding=utf-8 -*-
import os
import re
import file
import classV
import varV

class Parser:

	def __init__(self, path):
		self.f = open(path).readlines()
		baseName = os.path.basename(path)
		self.file = file.File(baseName)
		self.idx = 0

		pass
	
	def parse(self):
		print("xxxxxxxxxx")
		self.strip()
		self.parseDesc()
		self.parseNameSpace()
		self.parseHeadFile()
		self.parseClass()
		pass

	def strip(self):
		tmp = []
		for line in self.f:
			line = line.strip()
			if line == "":
				continue
			tmp.append(line)

		self.f = tmp
		#print(self.f)

	def delParsed(self, dels):
		idx = -1
		tmp = []
		for line in self.f:
			idx += 1
			if idx in dels:
				continue
			tmp.append(line)
		self.f = tmp

	def parseDesc(self):
		idx = -1
		dels = []
		for line in self.f:
			idx += 1
			dels.append(idx)
			matchObj = re.match(r"namespace(.+)", line)	
			if matchObj:
				namespace = matchObj.group(1).strip().strip(";")
				self.file.addNameSpace(namespace)
				break
			self.file.appendDesc(line)

		self.delParsed(dels)
		#print(self.f)
		pass

	
	def parseNameSpace(self):
		pass
	
	def parseHeadFile(self):
		idx = -1
		dls = []
		for line in self.f:
			idx += 1
			
			matchOb = re.match(r"import(.+)", line)
			if matchOb:
				headfile = matchOb.group(1).split(".")
				self.file.addHeadFile(headfile[0].strip()+".h")
				dls.append(idx)
				continue
			
			matchOb = re.match(r"message(.+)", line)
			if matchOb:
				break
		
		self.delParsed(dls)
		#print(self.f)
		pass

	def parseClass(self):
		desc = []
		idx = 0;
		while idx < len(self.f):
			line = self.f[idx]
			matchOb = re.match(r"message(.+)", line)
			if matchOb:
				lines = []
				
				while idx < len(self.f):
					line = self.f[idx]
					matchOb = re.match(r"^}", line)
					idx += 1
					lines.append(line)
					if matchOb:
						break
				self.parseMsg(desc, lines)
				pass
				msgline = matchOb.group(1)
				desc.append("//class " + msgline)
				msgName = msgline.split("=")[0].strip()
				print("//class " +matchOb.group(1) + msgName)
				
				idx += 1
				line = self.f[idx]
				matchOb = re.match(r"^{", line)
				if not matchOb:
					print("syntax error " + line)
					return

				idx += 1
				while idx < len(self.f):
					line = self.f[idx]
					if re.match(r"^}",line):
						idx += 1
						break

					matchOb = re.match(r"repeated(.+)", line)
					
					if matchOb:
						print(matchOb.group(1))
						ss = matchOb.group(1).strip().strip(";").split(" ")
						t = "vector<"+ss[0]+">"
						var = varV.Var(t, ss.[1])
						msg.appendVar(var)
					else:
						pass
					idx += 1


			else:
				desc.append(line)
				idx += 1


		msg = classV.Class()

	def parseMsg(self, desc, info):
		idx = 0
		line = info[idx]
		matchOb = re.match(r"message(.+)", line)
		msg = Class()
		if matchOb:
			msgline = matchOb.group(1)
			desc.append("//class " + msgline)
			msgName = msgline.split("=")[0].strip()
			msg.appendName(msgName)
		else:
			print("error")
			return False

		idx += 1
		line = info[idx]
		matchOb = re.match(r"^{", line)
		if not matchOb:
			print("error")
			return False
		
		idx += 1
		while idx < len(info):
			line = info[idx]

		pass




	


if __name__ == "__main__":
	parser = Parser("./msg_cg_skill.protocol")
	parser.parse()
